package com.dropdb.demo.service.impl;

import com.dropdb.demo.bean.Footprint;
import com.dropdb.demo.bean.FootprintExample;
import com.dropdb.demo.bean.bo.BaseData;
import com.dropdb.demo.bean.vo.FootprintListData;
import com.dropdb.demo.bean.vo.FootprintListElementData;
import com.dropdb.demo.mapper.FootprintMapper;
import com.dropdb.demo.mapper.FootprintMultiTableMapper;
import com.dropdb.demo.service.FootprintService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author:tanxin Desc:
 * @date:2020/6/27 1:02
 */
@Service
public class FootprintServiceImpl implements FootprintService {
    @Resource
    FootprintMapper footprintMapper;
    @Override
    public BaseData queryFootPrint(Integer page, Integer limit, String sort, String order, Integer userId, Integer goodsId) {
        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        //条件查询
        if (userId != null && goodsId != null){
            criteria.andUserIdEqualTo(userId).andGoodsIdEqualTo(goodsId);
        }else if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }else if (goodsId != null){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        footprintExample.setOrderByClause(sort + " " + order);
        //查询结果分页
        PageHelper.startPage(page,limit);

        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);

        PageInfo<Footprint> pageInfo = new PageInfo<>(footprints);
        long total = pageInfo.getTotal();
        return new BaseData(total,footprints);
    }



    //前台功能
    @Autowired
    FootprintMultiTableMapper footprintMultiTableMapper;

    /**
     * 获取用户足迹
     * @param userId
     * @return
     */
    @Override
    public FootprintListData footprintList(Integer userId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<FootprintListElementData> list = footprintMultiTableMapper.list(userId);
        long total = new PageInfo<>(list).getTotal();
        return new FootprintListData(total, list);
    }

    /**
     * 删除用户足迹
     * @param userId
     * @param id
     * @return
     */
    @Override
    public int deleteFootprint(Integer userId, Integer id) {
        Footprint footprint = new Footprint();
        footprint.setDeleted(true);
        FootprintExample footprintExample = new FootprintExample();
        footprintExample.createCriteria()
                .andIdEqualTo(id)
                .andUserIdEqualTo(userId);
        return footprintMapper.updateByExampleSelective(footprint, footprintExample);
    }
}
